//
//  TitleScrollView.m
//  PASystem
//
//  Created by 刘沿 on 16/8/25.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "TitleScrollView.h"

#define DEVICEWIDTH [UIScreen mainScreen].bounds.size.width
#define BTNWIDTH 120.0

@interface TitleScrollView()
{
    int _currentIndex;
}
@property (nonatomic,strong)UIScrollView *scrollView;

@end

@implementation TitleScrollView

-(instancetype)initWithArray:(NSArray *)arr{
    if (self = [super init]) {
        //不显示垂直滚动条
        self.showsHorizontalScrollIndicator = NO;
        self.headArray = arr;
        [self createUI];
        
    }

    return self;
}

- (void)createUI{
    self.backgroundColor = [UIColor whiteColor];
    self.contentSize = CGSizeMake(BTNWIDTH*self.headArray.count, 0);
    //是否会显示contentsize以外的内容
    self.bounces = NO;
    
    [self createBtn];
}

-(void)createBtn{
    
    CGFloat ViewW = self.frame.size.width;
    CGFloat w = 0;//保存前一个button的宽以及前一个button距离屏幕边缘的距离
    CGFloat h = 0;//用来控制button距离父视图的高
    
    for (int i = 0; i < [self.headArray count]; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        //计算大小
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:18]};
        CGFloat length = [self.headArray[i] boundingRectWithSize:CGSizeMake(ViewW, 100) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        
        button.frame = CGRectMake(w +10, h, length , 44);
        
        w = button.frame.size.width + button.frame.origin.x;
        
        button.titleLabel.font = [UIFont systemFontOfSize:16];
        
        
        [button setTitle:[self.headArray objectAtIndex:i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        if (i == 0) {
            [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        }
        button.tag = i + 100;
        [button addTarget:self action:@selector(didClickHeadButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:button];
        
    }
}

//按钮点击事件
-(void)didClickHeadButtonAction:(UIButton *)btn{
    _currentIndex = (int)btn.tag;
    [self changeBtnTitleColorWith:_currentIndex];
    
    if ([self.SelectDelegate respondsToSelector:@selector(selectControllerWithBtn:)]) {
        
        [self.SelectDelegate selectControllerWithBtn:btn];
    }
}

//改变按钮的颜色字体大小
-(void)changeBtnTitleColorWith:(int)tag{
    
    // 遍历的方法
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isKindOfClass:[UIButton class]]) {
            //遍历出来的按钮是点击的按钮
            if (obj.tag == tag) {
                [obj setTitleColor:[UIColor purpleColor] forState:UIControlStateNormal];
                if (obj.frame.origin.x>=DEVICEWIDTH/2&&obj.frame.origin.x<=self.contentSize.width-DEVICEWIDTH/2) {
                    [UIView animateWithDuration:.5 animations:^{
                        self.contentOffset = CGPointMake(obj.frame.origin.x-DEVICEWIDTH/2+40, 0);
                        obj.titleLabel.font = [UIFont systemFontOfSize:18];
                    }];
                }
                if (obj.frame.origin.x<DEVICEWIDTH/2) {
                    [UIView animateWithDuration:.5 animations:^{
                        self.contentOffset = CGPointMake(0, 0);
                        obj.titleLabel.font = [UIFont systemFontOfSize:18];
                    }];
                }
                
            }else{
                [obj setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
                obj.titleLabel.font = [UIFont systemFontOfSize:16];
            }
        }
        
    }];
    
    
}


@end
